package com.decent.manager.system.dao;

import com.decent.common.annotation.ReturnCheck;
import com.decent.manager.system.dao.provider.SystemRoleDaoProvider;
import com.decent.manager.system.dto.SystemRoleDTO;
import com.decent.manager.system.vo.SystemRoleVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 角色sql管理
 *
 * @author wangyan
 **/
@Mapper
public interface SystemRoleDao {
    /**
     * 查询角色列表
     *
     * @return list
     */
    @Select("select id, " +
            "       role_name, " +
            "       role_code, " +
            "       role_sort, " +
            "       remark, " +
            "       role_status, " +
            "       create_time, " +
            "       update_time " +
            "from system_role " +
            " order by role_sort")
    List<SystemRoleVO> getRoleList();

    /**
     * 判断是否存在重复名称或编码的角色
     *
     * @param role 查询信息
     * @return true：有重复；false：无重复
     */
    @Select("<script>" +
            "select 1 " +
            "from  system_role " +
            "where  role_status = #{role.roleStatus} " +
            "  and (role_name = #{role.roleName} or role_code = #{role.roleCode}) " +
            "<if test =\"role.id!=null \">" +
            " and id &lt;&gt; #{role.id} " +
            "</if>" +
            "limit 1" +
            "</script>")
    Boolean existRepeatRole(@Param("role") SystemRoleDTO role);

    /**
     * 创建角色
     *
     * @param role 新增信息
     * @return 新增行数
     */
    @SuppressWarnings("UnusedReturnValue")
    @Insert("insert into  system_role  " +
            "    (role_name, role_code, create_time, creator, update_time, last_modifier)  " +
            "    value  " +
            "    (#{roleName}, #{roleCode}, now(), #{creator}, now(), #{creator})")
    @ReturnCheck(info = "角色创建失败")
    int createRole(SystemRoleDTO role);

    /**
     * 修改角色
     *
     * @param role 修改信息
     * @return 修改行数
     */
    @SuppressWarnings("UnusedReturnValue")
    @UpdateProvider(type = SystemRoleDaoProvider.class, method = "updateRole")
    @ReturnCheck(info = "角色创建失败")
    int updateRole(@Param("role") SystemRoleDTO role);
}
